package com.qzsoft.zhjd.materiel.modules.materielitem.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.system.modules.zhjdsysdept.entity.ZhjdSysDeptEntity;
import com.qzsoft.system.modules.zhjdsysdept.service.ZhjdSysDeptService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseListDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseSaveDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.dto.MaterielItemBaseSelectDto;
import com.qzsoft.zhjd.materiel.modules.materielitem.entity.MaterielItemBaseEntity;
import com.qzsoft.zhjd.materiel.modules.materielitem.entity.MaterielItemEntity;
import com.qzsoft.zhjd.materiel.modules.materielitem.mapper.MaterielItemBaseMapper;
import com.qzsoft.zhjd.materiel.modules.materielitem.service.MaterielItemBaseService;
import com.qzsoft.zhjd.materiel.modules.materielitem.service.MaterielItemService;

/**
 * @ClassName MaterielItemBaseServiceImpl
 * @Description 养护基地材料字典业务接口实现
 * @author linhuawei
 * @since 2022-03-30
 */
@Service
public class MaterielItemBaseServiceImpl extends ServiceImpl<MaterielItemBaseMapper, MaterielItemBaseEntity>
        implements MaterielItemBaseService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private ZhjdSysDeptService zhjdSysDeptService;

    @Autowired
    private MaterielItemService materielItemService;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielItemBaseListDto> page(Map<String, Object> params) {
        IPage<MaterielItemBaseListDto> page = baseServiceComponent.getPage(params, MaterielItemBaseListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public List<MaterielItemBaseSelectDto> getBind(String baseCode) {
        List<MaterielItemBaseSelectDto> dataList = baseMapper.getBind(baseCode);
        return ConvertDictUtils.formatDicDataList(dataList);
    }

    @Override
    public List<MaterielItemBaseSelectDto> getUnbind(String baseCode) {
        List<MaterielItemBaseSelectDto> dataList = baseMapper.getUnbind(baseCode);
        return ConvertDictUtils.formatDicDataList(dataList);
    }

    @Override
    public List<MaterielItemBaseEntity> getByBaseCode(String baseCode) {
        QueryWrapper<MaterielItemBaseEntity> queryWrapper = new QueryWrapper<MaterielItemBaseEntity>();
        queryWrapper.eq("base_code", baseCode);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> saveReturnErrors(MaterielItemBaseSaveDto dto) {
        // 判断基地是否存在
        String baseCode = dto.getBaseCode();
        ZhjdSysDeptEntity base = zhjdSysDeptService.getBaseByCode(baseCode);
        if (null == base) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "基地[" + baseCode + "]不存在");
        }

        // 执行添加操作
        List<String> itemList = dto.getItemIdList();
        List<String> errors = new ArrayList<String>();
        if (ObjectUtils.isNotEmpty(itemList)) {
            Set<String> set = new HashSet<String>();
            MaterielItemBaseEntity entity;
            MaterielItemEntity itemEntity;
            Integer count;
            QueryWrapper<MaterielItemBaseEntity> queryWrapper = new QueryWrapper<MaterielItemBaseEntity>();
            for (String itemId : itemList) {
                // 数据重复，不做处理
                if (set.contains(itemId)) {
                    continue;
                }

                // 保存数据到set中，用户后续去重
                set.add(itemId);

                try {
                    // 判断材料是否存在
                    itemEntity = materielItemService.getItemByCustomId(itemId);
                    if (null == itemEntity) {
                        errors.add(itemId);
                        continue;
                    }

                    // 判断材料是否绑定
                    queryWrapper.clear();
                    queryWrapper.eq("materiel_item_id", itemId);
                    queryWrapper.eq("base_code", baseCode);
                    count = baseMapper.selectCount(queryWrapper);
                    if (null != count && 0 < count) {
                        errors.add(itemId);
                        continue;
                    }

                    // 执行添加操作
                    entity = new MaterielItemBaseEntity();
                    entity.setBaseCode(baseCode);
                    entity.setMaterielItemId(itemId);
                    baseMapper.insert(entity);
                } catch (Exception e) {
                    errors.add(itemId);
                }
            }
        }

        // 返回结果
        return errors;
    }

    @Override
    public void deleteByMaterielItemId(String materielItemId) {
        QueryWrapper<MaterielItemBaseEntity> queryWrapper = new QueryWrapper<MaterielItemBaseEntity>();
        queryWrapper.eq("materiel_item_id", materielItemId);
        baseMapper.delete(queryWrapper);
    }

}
